<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
<!--
  Author: jananir@google.com (Janani Ravi)
-->
<head>
<title>Closure Unit Tests - goog.net.BulkLoader</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.net.BulkLoader');
  goog.require('goog.events.EventHandler');
  goog.require('goog.events.EventType');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.MockClock');
</script>
</head>
<body>
<script type="text/javascript">

  /**
   * Test interval between sending uri requests to the server.
   */
  var DELAY_INTERVAL_BETWEEN_URI_REQUESTS = 5;

  /**
   * Test interval before a response is received for a URI request.
   */
  var DELAY_INTERVAL_FOR_URI_LOAD = 15;

  var clock;
  var loadSuccess, loadError;
  var successResponseTexts;

  function setUpPage() {
    clock = new goog.testing.MockClock(true);
  }

  function tearDownPage() {
    clock.dispose();
  }

  function setUp() {
    loadSuccess = false;
    loadError = false;
    successResponseTexts = [];
  }

  /**
   * Gets the successful bulkloader for the specified uris with some
   * modifications for testability.
   * <ul>
   *   <li> Added onSuccess methods to simulate success while loading uris.
   *   <li> The send function of the XhrManager used by the bulkloader
   *        calls the onSuccess function after a specified time interval.
   * </ul>
   * @param {Array.<string>} uris The URIs.
   */
  function getSuccessfulBulkLoader(uris) {
    var bulkLoader = new goog.net.BulkLoader(uris);
    bulkLoader.load = function() {
      var uris = this.helper_.getUris();
      for (var i = 0; i < uris.length; i++) {
        // This clock tick simulates a delay for processing every URI.
        clock.tick(DELAY_INTERVAL_BETWEEN_URI_REQUESTS);
        // This timeout determines how many ticks after the send request
        // all the URIs will complete loading. This delays the load of
        // the first uri and every subsequent uri by 15 ticks.
        setTimeout(goog.bind(this.onSuccess, this, i, uris[i]),
            DELAY_INTERVAL_FOR_URI_LOAD);
      }
    };

    bulkLoader.onSuccess = function(id, uri) {
      var xhrIo = {
        getResponseText: function() {return uri;},
        isSuccess: function() {return true;},
        dispose: function() {}
      };
      this.handleEvent_(id, new goog.events.Event(
          goog.net.EventType.COMPLETE, xhrIo));
    };

    var eventHandler = new goog.events.EventHandler();
    eventHandler.listen(bulkLoader,
        goog.net.EventType.SUCCESS,
        handleSuccess);
    eventHandler.listen(bulkLoader,
        goog.net.EventType.ERROR,
        handleError);

    return bulkLoader;
  };

  /**
   * Gets the non-successful bulkloader for the specified uris with some
   * modifications for testability.
   * <ul>
   *   <li> Added onSuccess and onError methods to simulate success and error
   *        while loading uris.
   *   <li> The send function of the XhrManager used by the bulkloader
   *        calls the onSuccess or onError function after a specified time
   *        interval.
   * </ul>
   * @param {Array.<string>} uris The URIs.
   */
  function getNonSuccessfulBulkLoader(uris) {
    var bulkLoader = new goog.net.BulkLoader(uris);
    bulkLoader.load = function() {
      var uris = this.helper_.getUris();
      for (var i = 0; i < uris.length; i++) {
        // This clock tick simulates a delay for processing every URI.
        clock.tick(DELAY_INTERVAL_BETWEEN_URI_REQUESTS);

        // This timeout determines how many ticks after the send request
        // all the URIs will complete loading in error. This delays the load
        // of the first uri and every subsequent uri by 15 ticks. The URI
        // with id == 2 is in error.
        if (i != 2) {
          setTimeout(goog.bind(this.onSuccess, this, i, uris[i]),
              DELAY_INTERVAL_FOR_URI_LOAD);
        } else {
          setTimeout(goog.bind(this.onError, this, i, uris[i]),
              DELAY_INTERVAL_FOR_URI_LOAD);
        }
      }
    };

    bulkLoader.onSuccess = function(id, uri) {
      var xhrIo = {
        getResponseText: function() {return uri;},
        isSuccess: function() {return true;},
        dispose: function() {}
      };
      this.handleEvent_(id, new goog.events.Event(
          goog.net.EventType.COMPLETE, xhrIo));
    };

    bulkLoader.onError = function(id) {
      var xhrIo = {
        getResponseText: function() {return null;},
        isSuccess: function() {return false;},
        dispose: function() {}
      };
      this.handleEvent_(id, new goog.events.Event(
          goog.net.EventType.ERROR, xhrIo));
    };

    var eventHandler = new goog.events.EventHandler();
    eventHandler.listen(bulkLoader,
        goog.net.EventType.SUCCESS,
        handleSuccess);
    eventHandler.listen(bulkLoader,
        goog.net.EventType.ERROR,
        handleError);

    return bulkLoader;
  };

  function handleSuccess(e) {
    loadSuccess = true;
    successResponseTexts = e.target.getResponseTexts();
  }

  function handleError(e) {
    loadError = true;
  }

  /**
   * Test successful loading of URIs using the bulkloader.
   */
  function testBulkLoaderLoadSuccess() {
    var uris = ['a', 'b', 'c'];
    var bulkLoader = getSuccessfulBulkLoader(uris);

    bulkLoader.load();

    clock.tick(2);
    assertFalse(
        'The bulk loader is not yet loaded (after 2 ticks)', loadSuccess);

    clock.tick(3);
    assertFalse(
        'The bulk loader is not yet loaded (after 5 ticks)', loadSuccess);

    clock.tick(5);
    assertFalse(
        'The bulk loader is not yet loaded (after 10 ticks)', loadSuccess);

    clock.tick(5);
    assertTrue('The bulk loader is loaded (after 15 ticks)', loadSuccess);

    assertArrayEquals('Ensure that the response texts are present',
        successResponseTexts, uris);
  }

  /**
   * Test error loading URIs using the bulkloader.
   */
  function testBulkLoaderLoadError() {
    var uris = ['a', 'b', 'c'];
    var bulkLoader = getNonSuccessfulBulkLoader(uris);

    bulkLoader.load();

    clock.tick(2);
    assertFalse(
        'The bulk loader is not yet loaded (after 2 ticks)', loadError);

    clock.tick(3);
    assertFalse(
        'The bulk loader is not yet loaded (after 5 ticks)', loadError);

    clock.tick(5);
    assertFalse(
        'The bulk loader is not yet loaded (after 10 ticks)', loadError);

    clock.tick(5);
    assertFalse(
        'The bulk loader is not loaded successfully (after 15 ticks)',
        loadSuccess);
    assertTrue(
        'The bulk loader is loaded in error (after 15 ticks)', loadError);
  }
</script>
</body>
</html>
